<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 药品采购
                </el-breadcrumb-item>
                <el-breadcrumb-item>供应商信息</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.name" placeholder="请输入供应商名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="supplierId" label="编号" width="50" align="center"></el-table-column>
                <el-table-column prop="supplierName" label="名称"></el-table-column>
                <el-table-column prop="supplierPrincipal" label="负责人"></el-table-column>
                <el-table-column prop="supplierPhone" label="联系方式"></el-table-column>
                <el-table-column prop="supplierAddress" label="地址"></el-table-column>
                <el-table-column prop="createTime" label="添加时间"></el-table-column>
                <el-table-column prop="updateTime" label="修改时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center" v-if="modifyTag">
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">修改
                        </el-button>
                        <el-button type="text" icon="el-icon-delete" class="red"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.current"
                    :page-size="query.pageSize" :total="query.total" @current-change="handlePageChange"></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="修改" v-model="editVisible" width="30%">
            <el-form label-width="70px">
                <el-form-item label="编号">
                    <el-input v-model="form.supplierId" disabled></el-input>
                </el-form-item>
                <el-form-item label="名称">
                    <el-input v-model="form.supplierName"></el-input>
                </el-form-item>
                <el-form-item label="负责人">
                    <el-input v-model="form.supplierPrincipal"></el-input>
                </el-form-item>
                <el-form-item label="联系方式">
                    <el-input v-model="form.supplierPhone"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.supplierAddress"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import { onActivated, ref, reactive, inject } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getSupplierList, modifySupplier, deleteSupplier } from "../../api";

export default {
    name: "supplier",
    setup() {
        const query = reactive({
            current: 1,
            pageSize: 10,
            total: 0,
            name: null
        });
        const tableData = ref([]);
        const pageTotal = ref(0);
        const reload  = inject("reload");
        // let tableData = [];
        // 获取表格数据
        const getData = () => {
            getSupplierList(query).then((res) => {
                tableData.value = res.data.message;
                query.total = res.data.total;
            });
        };
        getData();

        let modifyTag = true;
        const initTag = () => {
            let role = localStorage.getItem("role");
            if (role === 1 || role === '1' || role === 2 || role === '2' || role === 3 || role === '3' ) {
                modifyTag = false;
            }
        };
        initTag();

        // 查询操作
        const handleSearch = () => {
            query.current = 1;
            getData();
        };
        // 分页导航
        const handlePageChange = (val) => {
            query.current = val;
            getData();
        };

        // 删除操作
        const handleDelete = (index, row) => {
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {
                type: "warning",
            }).then(() => {
                deleteSupplier(row.supplierId).then(res => {
                    if (res.code === 200) {
                        reload();
                        ElMessage.success('删除成功');
                    } else {
                        console.log("[删除供应商] 失败: " + res.message);
                        ElMessage.error('服务器繁忙，请稍后重试');
                    }
                })
            }).catch(() => {});
        };

        // 表格编辑时弹窗和保存
        const editVisible = ref(false);
        let form = reactive({
            supplierId: 0,
            supplierName: '',
            supplierPrincipal: '',
            supplierPhone: '',
            supplierAddress: ''
        });
        const handleEdit = (index, row) => {
            Object.keys(form).forEach((item) => {
                form[item] = row[item];
            });
            editVisible.value = true;
        };
        const saveEdit = () => {
            modifySupplier(form).then(res => {
                if (res.code === 200) {
                    reload();
                    ElMessage.success('修改成功');
                } else {
                    console.log("[修改供应商] 失败: " + res.message);
                    ElMessage.error('服务器繁忙，请稍后重试');
                }
            });
            editVisible.value = false;
        };

        onActivated(() => {
            console.log("Supplier onActivated!");
            getData();
        });

        return {
            query,
            tableData,
            pageTotal,
            editVisible,
            form,
            modifyTag,
            handleSearch,
            handlePageChange,
            handleDelete,
            handleEdit,
            saveEdit,
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
</style>
